//
//  SuccessViewController.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/8/20.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit
import RxSwift

class SuccessViewController: BaseUIViewController {
    
    let result: CheckResultModel
    
    init(result: CheckResultModel) {
        self.result = result
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tableView = UITableView.init(frame: .zero, style: .grouped).config
            .backgroundColor(.white)
            .separatorStyle(.none)
            .register(SuccessCell.self)
            .addTo(view)
            .makeConstraints { (make) in
                make.edges.equalToSuperview()
        }.view
        
        let headerView = UIView.init(frame: CGRect(x: 0, y: 0,
                                                   width: SCREEN_WIDTH,
                                                   height: SCREEN_WIDTH * 0.63 + 10))
            .config
            .backgroundColor(#colorLiteral(red: 0.9606800675, green: 0.9608443379, blue: 0.9606696963, alpha: 1))
            .view
        
        UIImageView().config
            .frame(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_WIDTH * 0.63)
            .backgroundColor(.white)
            .clipsToBounds(true)
            .contentMode(.scaleAspectFill)
            .image(#imageLiteral(resourceName: "success_background"))
            .addTo(headerView)
        
        tableView.tableHeaderView = headerView
        tableView.tableFooterView = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 30))
        
        var datArray: [(name: String, value: String)] = []
        
        switch result.edition {
        case .V1, .HCB:
            datArray = [(name: "序列号".localized + ":", value: result.barcode),
                        (name: "产品类型".localized + ":", value: result.model),
                        (name: "生产时间".localized + ":", value: result.produceTime),
                        (name: "收货地".localized + ":", value: result.address),
                        (name: "技术所属".localized + ":", value: result.echnology),
                        (name: "等级".localized + ":", value: result.grade)]
        default:
            datArray = [(name: "序列号".localized + ":", value: result.barcode),
                        (name: "产品类型".localized + ":", value: result.model),
                        (name: "电池片类型".localized + ":", value: result.echnology),
//                        (name: "产地".localized + ":", value: result.provenance),
                        (name: "生产时间".localized + ":", value: result.produceTime),
                        (name: "收货地".localized + ":", value: result.address),
                        (name: "等级".localized + ":", value: result.grade)]
        }
        
        Observable.just(datArray)
            .bind(to: tableView.rx.items(cellType: SuccessCell.self)) { (_, model, cell) in
                cell.textLabel?.text = model.name
                cell.detailTextLabel?.text = model.value
        }.disposed(by: rx.disposeBag)
        
        tableView.rx.setDelegate(self)
            .disposed(by: rx.disposeBag)
    }
    
    override func navigationBarStyle() -> NavigationBarStyle {
        return .baseColor
    }
    
}

extension SuccessViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 70
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView().config
            .frame(x: 0, y: 0, width: SCREEN_WIDTH, height: 50)
            .backgroundColor(.white)
            .view
        
        let leftSepator = UIView().config
            .backgroundColor(APPConfig.baseColor)
            .addTo(view)
            .makeConstraints { (make) in
                make.left.equalTo(15)
                make.centerY.equalTo(view.snp.centerY)
                make.width.equalTo(3)
                make.height.equalTo(20)
        }.view
        
        UIView().config
            .backgroundColor(#colorLiteral(red: 0.9488047957, green: 0.9488047957, blue: 0.9488047957, alpha: 1))
            .addTo(view)
            .makeConstraints { (make) in
                make.left.bottom.right.equalTo(0)
                make.height.equalTo(1)
        }
        
        UILabel().config
            .text("查验结果".localized)
            .textColor(APPConfig.baseColor)
            .font(UIFont.systemFont(ofSize: 17, weight: .bold))
            .addTo(view)
            .makeConstraints { (make) in
                make.left.equalTo(leftSepator.snp.right).offset(20)
                make.centerY.equalTo(leftSepator.snp.centerY)
        }
        
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 80
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let height = self.tableView(tableView, heightForFooterInSection: section)
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: height))
        
        UILabel().config
            .text("感谢您采购新源正品组件！".localized)
            .textColor(APPConfig.baseColor)
            .font(UIFont.systemFont(ofSize: 14, weight: .bold))
            .backgroundColor(#colorLiteral(red: 0.9921568627, green: 0.968627451, blue: 0.9294117647, alpha: 1))
            .textAlignment(.center)
            .addTo(view)
            .makeConstraints { (make) in
                make.left.equalTo(20)
                make.center.equalToSuperview()
                make.top.equalTo(height - 45)
                make.bottom.equalToSuperview()
        }
        
        return view
    }
    
}
